package com.syh.jn.scm.infrastructure.repository.impl.supplier.finance.archive;

import cn.redscarf.commons.wrapper.Restrictions;
import cn.redscarf.commons.wrapper.Wrapper;
import com.syh.core.ddd.infrastructure.repository.AggregateRepoHibernateImplTmpl;
import com.syh.core.expction.DataModelException;
import com.syh.jn.scm.domain.supplier.entity.finance.archive.Archive;
import com.syh.jn.scm.domain.supplier.entity.finance.archive.ArchiveSettlements;
import com.syh.jn.scm.domain.supplier.entity.finance.archive.ArchiveStock;
import com.syh.jn.scm.domain.supplier.repository.finance.archive.ArchiveRepository;
import com.syh.jn.scm.exception.BusinessException;
import com.syh.jn.scm.infrastructure.repository.model.finance.archive.ArchiveModel;
import com.syh.jn.scm.infrastructure.repository.model.finance.archive.ArchiveSettlementsModel;
import com.syh.jn.scm.infrastructure.repository.model.finance.archive.ArchiveStockModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by soci.song on 2019/3/1.
 */
@Repository
public class ArchiveRepoImpl implements ArchiveRepository {

    private AggregateRepoHibernateImplTmpl<Archive,ArchiveModel,Long> tmpl;

    private AggregateRepoHibernateImplTmpl<ArchiveSettlements,ArchiveSettlementsModel,Long> settlementsItemsTmpl;

    private AggregateRepoHibernateImplTmpl<ArchiveStock,ArchiveStockModel,Long> stockItemsTmpl;

    @Autowired
    public ArchiveRepoImpl(HibernateTemplate hibernateTemplate) {
        this.tmpl = new AggregateRepoHibernateImplTmpl.Builder()
                .hibernateTemplate(hibernateTemplate)
                .entityClass(Archive.class)
                .modelClass(ArchiveModel.class)
                .idClass(Long.class).build();

        this.settlementsItemsTmpl = new AggregateRepoHibernateImplTmpl.Builder()
                .hibernateTemplate(hibernateTemplate)
                .entityClass(ArchiveSettlements.class)
                .modelClass(ArchiveSettlementsModel.class)
                .idClass(Long.class).build();

        this.stockItemsTmpl = new AggregateRepoHibernateImplTmpl.Builder()
                .hibernateTemplate(hibernateTemplate)
                .entityClass(ArchiveStock.class)
                .modelClass(ArchiveStockModel.class)
                .idClass(Long.class).build();
    }

    @Override
    public Long add(Archive entity) {
        if(entity.getSettlementsItems() == null || entity.getSettlementsItems().isEmpty())
            throw new DataModelException("Archive SettlementsItems 不能为null！");
        if(entity.getStockItems() == null || entity.getStockItems().isEmpty())
            throw new DataModelException("Archive StockItems 不能为null！");

        ArchiveModel model = tmpl.add(entity);
        //应收付结存
        entity.getSettlementsItems().forEach(item ->{
            item.setArchiveId(model.getId());
            settlementsItemsTmpl.add(item);
        });
        //库存结存
        entity.getStockItems().forEach(item ->{
            item.setArchiveId(model.getId());
            stockItemsTmpl.add(item);
        });
        return model.getId();
    }

    @Override
    public void removeById(Long id) {
        throw new BusinessException("该接口暂未实现！");
    }

    @Override
    public void remove(Archive entity) {
        if(entity.getSettlementsItems() == null || entity.getSettlementsItems().isEmpty())
            throw new DataModelException("Archive SettlementsItems 不能为null！");
        if(entity.getStockItems() == null || entity.getStockItems().isEmpty())
            throw new DataModelException("Archive StockItems 不能为null！");

        tmpl.remove(entity);
        //应收付反结存
        entity.getSettlementsItems().forEach(item ->{
            settlementsItemsTmpl.remove(item);
        });
        //库存反结存
        entity.getStockItems().forEach(item ->{
            stockItemsTmpl.remove(item);
        });
    }

    @Override
    public Archive getById(Long id) {
        Archive entity = tmpl.getById(id);
        if(entity != null) {
            entity.setSettlementsItems(this.findSettlementsByArchiveId(entity.getId()));
            entity.setStockItems(this.findStockByArchiveId(entity.getId()));
        }
        return entity;
    }

    @Override
    public Archive getOne(Wrapper wrapper) {
        Archive entity = tmpl.getOne(wrapper);
        if(entity != null){
            entity.setSettlementsItems(this.findSettlementsByArchiveId(entity.getId()));
            entity.setStockItems(this.findStockByArchiveId(entity.getId()));
        }
        return entity;
    }

    @Override
    public boolean contains(Long id) {
        return tmpl.getById(id) != null;
    }

    @Override
    public List<Archive> getList(Wrapper wrapper) {
        return tmpl.getList(wrapper);
    }

    @Override
    public List<Archive> findListPage(Wrapper wrapper, Integer startRow, Integer count) {
        return tmpl.findListPage(wrapper,startRow,count);
    }

    @Override
    public int getCount(Wrapper wrapper) {
        return tmpl.getCount(wrapper);
    }

    @Override
    public List<ArchiveSettlements> findSettlementsByArchiveId(Long archiveId) {
        Wrapper wrapper = Wrapper.build().append(Restrictions.eq("archiveId",archiveId));
        return settlementsItemsTmpl.getList(wrapper);
    }

    @Override
    public List<ArchiveStock> findStockByArchiveId(Long archiveId) {
        Wrapper wrapper = Wrapper.build().append(Restrictions.eq("archiveId",archiveId));
        return stockItemsTmpl.getList(wrapper);
    }

    @Override
    public List<ArchiveSettlements> findSettlements(Wrapper wrapper) {
        return settlementsItemsTmpl.getList(wrapper);
    }

    @Override
    public List<ArchiveStock> findStock(Wrapper wrapper) {
        return stockItemsTmpl.getList(wrapper);
    }

    @Override
    public List<ArchiveSettlements> findSettlementsPage(Wrapper wrapper, Integer startRow, Integer count) {
        return settlementsItemsTmpl.findListPage(wrapper,startRow,count);
    }

    @Override
    public List<ArchiveStock> findStockPage(Wrapper wrapper, Integer startRow, Integer count) {
        return stockItemsTmpl.findListPage(wrapper,startRow,count);
    }

    @Override
    public Integer countSettlements(Wrapper we) {
        return settlementsItemsTmpl.getCount(we);
    }

    @Override
    public Integer countStock(Wrapper we) {
        return stockItemsTmpl.getCount(we);
    }
}
